package server;

import com.google.gson.Gson;
import dao.GoodsDao;
import dao.UserDao;
import imp.Goods_imp;
import imp.User_imp;
import model.Goods;
import model.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WebServer extends javax.servlet.http.HttpServlet  {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String msg = request.getParameter("msg");
        switch (msg) {
            case "adminlogin":
                adminlogin(request, response, out);
                break;
            case "home":
                home(request, response, out);
                break;
            default:
                break;
        }
    }
    private void adminlogin(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
//        out = response.getWriter();
//        out.write(request.getParameter("account"));
//        out.write("\r\n");
//        out.write(request.getParameter("password"));
//        out.write("\r\n");

        RequestDispatcher dispatcher = request.getRequestDispatcher("/webserver?msg=home");
        dispatcher.forward(request, response);
    }
    private void home(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
        String p = request.getParameter("page");
        int page;
        try {
            //当前页数
            page = Integer.valueOf(p);
        } catch (NumberFormatException e) {
            page = 1;
        }
        System.out.println("页数"+p);
        List<Goods> goodsList;
        GoodsDao goodsDao=new Goods_imp();
        goodsList=goodsDao.ShowGoods();
        System.out.println("goodsList的"+goodsList.size());
//        GoodsDao goods = new Goods_imp();
//        List<Goods> list;
//        list = goods.ShowGoods();
//        System.out.println("list的"+list.size());
        //物品总数
        int goodssize=goodsList.size();
        //每页显示物品数量
        int goodsPerPage = 10;
        //总页数
        int PagesSize = goodssize % goodsPerPage == 0 ? goodssize / goodsPerPage : goodssize / goodsPerPage + 1;
        //本页起始物品序号
        int beginIndex = (page - 1) * goodsPerPage;
        //本页末尾物品序号的下一个
        int endIndex = beginIndex + goodsPerPage;
        if (endIndex > goodssize)
            endIndex = goodssize;

        request.setAttribute("goodssize", goodssize);
        request.setAttribute("goodsPerPage", goodsPerPage);
        request.setAttribute("PagesSize", PagesSize);
        request.setAttribute("beginIndex", beginIndex);
        request.setAttribute("endIndex", endIndex);
        request.setAttribute("page", page);
        request.setAttribute("goodsList", goodsList);
        request.getRequestDispatcher("home.jsp").forward(request, response);
    }


}
